var url = null;
var formatDateStr = "yyyy-MM-dd";

$(function() {
	var nowDate = new Date();
	var lastDate = new Date(nowDate.valueOf() - 24 * 60 * 60 * 1000);
	
	var startTime = lastDate.format(formatDateStr);
	var endTime = lastDate.format(formatDateStr);
	$('#startTime_query').datebox('setValue', startTime);
	$('#endTime_query').datebox('setValue', endTime);
	
//	query();
});

function query() {
	var startTime = getMillSecond("startTime_query");
	var endTime = getMillSecond("endTime_query");
	
	$('#dg1').datagrid({
		url : getHttpRequestPath("/record/getMoodScoreTable"),
		pageNumber : 1,
		queryParams : {
			startTime : startTime,
			endTime : endTime
		}
	});
	queryGraph(startTime, endTime);
}

function queryGraph(startTime, endTime) {
	var dates = [];
	var isManagers = [];
	var notManagers = [];
	$.post(getHttpRequestPath("/record/getMoodScoreLine"), {
		startTime : startTime,
		endTime : endTime
	}, function(typesString) {
		JSON.parse(typesString, function(key, value) {
			if (key == 'recordDate') {
				var date = new Date(value).format(formatDateStr);
				dates.push(date);
			}
			if (key == 'isManagerScore') {
				isManagers.push(value);
			}
			if (key == 'notManagerScore') {
				notManagers.push(value);
			}
		});
		createLine(dates, isManagers, notManagers);
	}).error(_error);
}

function createLine(dates, isManagers, notManagers) {
	new Highcharts.Chart({
		chart: {
			renderTo: 'container',
			type: 'line',
			marginRight: 150,
			marginBottom: 30
		},
		title: {
			text: '数据曲线图表',
			x: -20	//center
		},
		xAxis: {
			categories: dates
		},
		yAxis: [{
			title: {
				text: '管理层平均分'
			},
			plotLines: [{
				value: 0,
				width: 2,
				color: '#808080'
			}]
		},{
			title: {
				text: '非管理层平均分'
			},
			opposite: true,
			plotLines: [{
				value: 0,
				width: 2,
				color: '#808080'
			}]
		}],
		tooltip: {
			formatter: function() {
				return '<b>' + this.series.name + '</b><br/>' + this.x + ' : ' + this.y;
			}
		},
		legend: {
			layout: 'vertical',
			align: 'right',
			verticalAlign: 'top',
			backgroundColor: '#FFFFFF',
			x: 5,
			y: 300,
			floating: true,
			shadow: true,
			borderWidth: 0
		},
		series: [{
			name: '管理层平均分',
			data: isManagers,
			yAxis: 0
		}, {
			name: '非管理层平均分',
			data: notManagers,
			yAxis: 1
		}]
	});
}

function formatDate(val, row, index) {
	var str = "";
//	str += new Date(row.recordDate).format(formatDateStr);
	var startTime = $('#startTime_query').datebox('getValue');
	var endTime = $('#endTime_query').datebox('getValue');
	str += startTime + ' 至 ' + endTime;
	return str;
}

function formatTotal(val, row, index) {
	var str = "";
	str += row.totalCount;
	return str;
}

function formatAverage(val, row, index) {
	var str = "";
	str += row.averageScore;
	return str;
}
